<?php
/**
 * @Author: rocky
 * Date: 2019/9/28
 * Time: 11:22
 */


namespace app\shop\controller\api\v1;

use app\common\controller\BaseApiController;
use app\common\model\ShopCoupon;
use app\common\model\ShopCouponRecord;

/**
 * 定时刷新任务
 * Class TimeCoupon
 * @package app\shop\controller\api\v1
 */
class TimeCoupon extends BaseApiController
{
    /**
     * 优惠券自动过期
     */
    public function getExpire()
    {
        $data = ShopCoupon::whereTime('end_time', '<=', time())->where('status',1)->select();
        if ($data){
            foreach ($data as $key =>$value){
                ShopCoupon::where('id',$value['id'])->setField('status',2);
                ShopCouponRecord::where('coupon_id',$value['id'])->setField('status',2);
            }
        }
        $this->successCode();
    }

    /**
     * 优惠券自动生效
     */
    public function getEffect()
    {
        $data = ShopCoupon::whereTime('start_time', '>=', time())->where('status',0)->select();
        if ($data){
            foreach ($data as $key =>$value){
                ShopCoupon::where('id',$value['id'])->setField('status',1);
                ShopCouponRecord::where('coupon_id',$value['id'])->setField('status',1);
            }
        }
        $this->successCode();
    }

}